package com.learn.controller;

import com.learn.common.Response;
import com.learn.entity.Customer;
import com.learn.entity.CustomerExample;
import com.learn.mybatis.mapper.CustomerMapper;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v3/")
@RequiresAuthentication
public class CustomerController {
    @Autowired
    CustomerMapper customerMapper;
    //        创建条件对象
    CustomerExample customerExample = new CustomerExample();
    //        创建规则对象
    CustomerExample.Criteria criteria = customerExample.createCriteria();

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/status")
    public Response<String> status() {
        Customer customer = new Customer();
        customer.setBalance(100.0);
        customerMapper.insert(customer);
        return Response.of("Successfully inserted a customer!");
    }

    @GetMapping("/customers")
    public Response<List<Customer>> getAllCustomer() {
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        return Response.success(customers);
    }
    @RequiresAuthentication
    @GetMapping("/customers/{id}")
    public Response<Customer> getCustomer(@PathVariable int id) {
        criteria.andIdEqualTo(id);
        List<Customer> customers = customerMapper.selectByExample(customerExample);
        return Response.success(customers.get(0));
    }

}